package com.david.cms.controller;
import java.io.IOException;
import java.util.Date;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.david.cms.model.Group;
import com.david.cms.model.Member;
import com.david.cms.model.Role;
import com.david.cms.service.IMemberService;
@RequestMapping("/member")
@Controller
public class PortalMemberController {
	private IMemberService memberService;
	@Inject
	public void setMemberservice(IMemberService memberService) {
		this.memberService = memberService;
	}
	
	@RequestMapping(value = "toRegister")
	public String toRegister(Model model) {
		model.addAttribute(new Member());
		return "../../portal_1/member/register";
	}
	
	@RequestMapping(value = "toLogin")
	public String toLogin(Model model) {
		model.addAttribute(new Member());
		return "../../portal_1/member/login";
	}
	@RequestMapping(value = "login")
	public String login(Member member,Model model,HttpServletRequest request) {
		Member membery=null;
		try {
			membery=this.memberService.searchMember(member.getLoginName(),member.getPassWord());
	
			HttpSession session=request.getSession();
			session.setAttribute("member", membery);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "../../portal_1/member/login";
		}
	
		return "../../portal_1/member/member_center";
	}
	
	
	
	@RequestMapping(value = "register",method=RequestMethod.POST)
	public String register(Member member) {
//		System.out.println(member==null);
		System.out.println(member.getRealName()+"-----------------------------");
		member.setRegisterDate(new Date());
		try {
		
			memberService.addMember(member);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "../../portal_1/member/register";
		}
		return "../../portal_1/member/login";
	}
	
	
	
	/**
	 * 验证该用户名是否已经被注册
	 */
	@RequestMapping(value = "checkName")
	public void checkMemberByLoginName(String loginID,HttpServletRequest request, HttpServletResponse response) throws IOException{
		  response.setContentType("text/xml;charset=utf-8");
          request.setCharacterEncoding("utf-8");
          response.setCharacterEncoding("utf-8");
		
		Member member=  memberService.searchMemberByLoginName(loginID);
		System.out.println(member==null);
		System.out.println("----------------sadasdsadas----------------"+loginID);
		if(member==null){
			
			response.getWriter().println(1);
		}else{
		
			response.getWriter().println(2);
		}
	
	}
	

}
